package com.rdc.qingkou.model;


import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.dto.CardDTO;
import com.rdc.qingkou.dto.ResultDTO;
import com.rdc.qingkou.model.imodel.IBrowseModel;
import com.rdc.qingkou.presenter.BrowsePresenter;
import com.rdc.qingkou.presenter.ipresenter.IBrowsePresenter;
import com.rdc.qingkou.retrofitService.IBrowseService;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.vo.CardAndUserVO;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;


public class BrowseModel implements IBrowseModel {

    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
    private IBrowsePresenter presenter;

    public BrowseModel(BrowsePresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getCardsByTag(String tag, int currentPage) {
        MyUtils.getRetrofitProvider(URL.GET_CARD_BY_TAG)
                .create(IBrowseService.class)
                .getCardByTag(tag, currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<List<CardDTO>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<List<CardDTO>> listResponse) {
                        if (listResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getCardError("获取卡片失败！error code :" + listResponse.code());
                            return;
                        }
                        List<CardAndUserVO> list = new ArrayList<>();
                        for (CardDTO card : listResponse.body()) {
                            list.add(card.transformToCardAndUser());
                        }
                        presenter.getCardSuccess(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getCardError("获取卡片失败！error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getCardByKey(int userId, int currentPage, String url) {
        MyUtils.getRetrofitProvider(url)
                .create(IBrowseService.class)
                .getCardByKey(userId, currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<List<CardDTO>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<List<CardDTO>> listResponse) {
                        if (listResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getCardError("获取卡片失败！error code :" + listResponse.code());
                            return;
                        }
                        List<CardAndUserVO> list = new ArrayList<>();
                        for (CardDTO card : listResponse.body()) {
                            list.add(card.transformToCardAndUser());
                        }
                        presenter.getCardSuccess(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getCardError("获取卡片失败！error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void Subscribe(int subscribeId, int userId) {
        MyUtils.getRetrofitProvider(URL.POST_SUBSCRIBE)
                .create(IBrowseService.class)
                .subscribe(subscribeId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<ResultDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<ResultDTO> resultDTOResponse) {
                        if (resultDTOResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.subscribeError("订阅/取消失败！error code:" + resultDTOResponse.code());
                        } else if (!resultDTOResponse.body().getResult().equals("success")) {
                            presenter.subscribeError("订阅/取消失败！reason:" + resultDTOResponse.body().getReason());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.subscribeError("订阅/取消失败！error :" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void clearDisposable() {
        mCompositeDisposable.dispose();
    }

    @Override
    public void addDisposable(Disposable disposable) {
        mCompositeDisposable.add(disposable);
    }
}
